using System.Collections.Generic;
using UnityEngine;

public class DialogUI_Selection : MyCanvasGrounp
{
    private DialogSystem dialogSystem;

    [SerializeField]
    private Transform contentTransform;
    private const int NUM_BUTTON = 7;
    private readonly List<GameObject> buttons = new List<GameObject>();

    protected override void Awake()
    {
        base.Awake();
        dialogSystem = GetComponentInParent<DialogSystem>();
        dialogSystem.ShowSelection += OnShowSelection;
        dialogSystem.EndSelection += OnEndSelection;
        GenerateButtons();
        Visible = false;
    }

    private void GenerateButtons()
    {
        GameObject prototype = GetComponentInChildren<DialogUI_SelectionButton>().gameObject;
        prototype.SetActive(false);
        for (int i = 0; i < NUM_BUTTON; ++i)
        {
            buttons.Add(Instantiate(prototype, contentTransform));
        }
        Destroy(prototype);
    }

    private void OnShowSelection(List<string> list, DialogNode[] nodes)
    {
        GameObject obj;
        int i = 0;
        for (; i < list.Count; ++i)
        {
            obj = buttons[i];
            obj.SetActive(true);
            obj.GetComponent<DialogUI_SelectionButton>().ShowSelection(list[i], nodes[i]);
        }
        for (; i < buttons.Count; ++i)
        {
            buttons[i].SetActive(false);
        }
        Visible = true;
    }


    private void OnEndSelection()
    {
        Visible = false;
    }
}
